const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    text: '',
    index: 0,
    fe_id:'',
    imgindex: [],
    array: [
      '会员卡',
      '其他',
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.getStorage({
      key: 'openid',
      success: function(res) {
        let openid = res.data

        wx.request({
          url: app.globalData.URL + '/app/feedback/add?type=1&openid=' + openid,
          success: function (res) {
            console.log("co_id:", res.data)
            that.setData({
              fe_id: res.data.fe.id,
            })
          }
        })
      },
    })
    
  },
  nameInput: function (e) {
    this.setData({
      name: e.detail.value
    })
    console.log("name", this.data.name)
  },
  textInput:function(e){
    this.setData({
      text: e.detail.value
    })
    console.log("name", this.data.text)
  },
  selectType: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  
  
 
  bindChooiceProduct: function () {
    let that = this;
    wx.chooseImage({
      count: 1,  //最多可以选择的图片总数
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success(res) {
        wx.showLoading({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 8000
        })
        if (res.errMsg == 'chooseImage:ok') {
          wx.hideLoading()
          console.log(res);
          const tempFilePaths = res.tempFilePaths;
          var arr = [];
          tempFilePaths.forEach((value, indexindex) => {
            arr.push(value)
          })
          that.setData({
            imgindex: arr
          })
          console.log(that.data.imgindex)

          wx.getStorage({
            key: 'openid',
            success: function(res) {
              let openid = res.data

              wx.uploadFile({
                url: app.globalData.URL + '/app/feedback/imageUpload?openid='+ openid,
                filePath: that.data.imgindex[0],
                name: 'file',
                header: {
                  "Content-Type": "multipart/form-data"
                },
                formData: {
                  "uid": that.data.fe_id,
                },
                success: function (res) {
                  console.log('feedback/imageUpload',res)
                  that.setData({
                    errMsg: res.errMsg
                  })

                  if (that.data.errMsg == "uploadFile:ok" ) {
                    wx.showToast({
                      title: '上传成功',
                      duration: 3000
                    })
                  } else {
                    wx.showToast({
                      title: '上传失败',
                      image: '../../images/error.png'
                    })
                  }
                }
              })
            },
          })
          
        }
      }
    })
  },
  //长按事件删除图片
  removeimge(e) {
    const that = this;
    let index = e.currentTarget.dataset.index;
    let imgindex = that.data.imgindex;
    imgindex.splice(index, 1);
    wx.showModal({
      title: '提示',
      content: '确定要删除此图片吗？',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          that.setData({
            imgindex: imgindex
          })
          wx.showToast({
            title: '删除成功',
            icon: 'none'
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  previewImg: function (e) {
    var current = e.target.dataset.src;
    wx.previewImage({
      current: current,
      urls: this.data.imgindex,
      success: function (res) {
        console.log('success');
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  }, //转发
  f: function () {
    console.log("f");
  },
  s: function () { console.log("s"); },
  deleteImage: function (e) {
    var that = this;
    var images = that.data.imgs;
    console.log(images);
    var index = e.currentTarget.dataset.index; //获取当前长按图片下标
    console.log(index);
    wx.showModal({
      title: '系统提醒',
      content: '确定要删除此图片吗？',
      success: function (res) {
        if (res.confirm) {
          images.splice(index, 1);
        } else if (res.cancel) {
          return false;
        }
        that.setData({
          files: images
        });
      }
    })
  },

  submit: function () {
    var that = this
    if (this.data.name == '') {
      wx.showToast({
        title: '请输入名字',
        image: '../../images/error.png'
      })
    } else if (this.data.phone == "") {
      wx.showToast({
        title: '请输入手机号',
        image: '../../images/error.png'
      })
    } else if (this.data.text == "") {
      wx.showToast({
        title: '请输入您的建议',
        image: '../../images/error.png'
      })
    } else {
      wx.getStorage({
        key: 'openid',
        success: function(res) {
          console.log("getStorage",res)
          let openid = res.data

          var com = {
            id: that.data.co_id,
            productName: that.data.name,
            content: that.data.text,
            opinionType: that.data.index
          }
          var data = { 'com': com, 'type': 0, 'openid': openid }
          console.log("参数", data)

          wx.request({
            url: app.globalData.URL + '/app/feedback/addFeedback',
            method: 'post',
            data: JSON.stringify(data),
            header: {
              "Content-Type": "application/json;charset=utf-8"
            },
            success: function (res) {
              if (res.data.flag == true) {
                wx.showModal({
                  title: '',
                  content: '提交成功',
                  success: function () {
                    wx.navigateBack({
                    })
                  }
                })
              } else {
                wx.showToast({
                  title: '提交失败',
                  image: '../../images/error.png'
                })
              }
            }
          })
        },
      })
        
    }

  }
})